﻿using Microsoft.VisualBasic.Logging;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Navigation.Regions;
using System;
using System.Configuration;
using System.Data;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using ZuesControls.Controls;
using ZuesUI.Commons.MyPrism;
using ZuesUI.ViewModels;
using ZuesUI.Views;
using TreeListView = ZuesUI.Views.TreeListView;

namespace ZuesUI
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : MyPrismApplication
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            //TextEditorThemeHelper.SetCurrentTheme("Dark");
            //全局错误处理
            //System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.Current.DispatcherUnhandledException += Application_DispatcherUnhandledException;
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;//处理在Task上未处理的异常
           
        }

        private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
        {
            try
            {
                var exception = e.Exception;
                if (exception != null)
                {
                    //log.Error(exception);
                }
            }
            catch (Exception ex)
            {
                //log.Error(ex);
            }
            finally
            {
                //ignore
            }
            //记录严重错误     
        }

        private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            //log.Error(e.Exception);
            Application.Current.Dispatcher.Invoke((Action)delegate ()
            {
                System.Windows.Forms.MessageBox.Show(e.Exception.Message);
            });
            e.Handled = true;//使用这一行代码告诉运行时，该异常被处理了，不再作为UnhandledException抛出了。  
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                var exception = e.ExceptionObject as Exception;
                if (exception != null)
                {
                  
                    //log.Error(exception);
                }
            }
            catch (Exception ex)
            {
                //log.Error(ex);
            }
            finally
            {
                //ignore
            }
            //记录严重错误  
        }


        protected override Window CreateShell()
        {
            var window = Container.Resolve<MainWindow>();
            return window;
        }

        //Prism框架下的区域适配器
        protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
        {

            base.ConfigureRegionAdapterMappings(regionAdapterMappings);

            // 注册 ContentControl 的区域适配器

        }

        protected override void InitializeShell(Window shell)
        {
            base.InitializeShell(shell);
        }
        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            var homePageModule = typeof(HomeModule);
            moduleCatalog.AddModule(new ModuleInfo()
            {
                ModuleName = homePageModule.Name,
                ModuleType = homePageModule.AssemblyQualifiedName,
                InitializationMode = InitializationMode.WhenAvailable
            });
        }
        //Prism框架下的视图和视图模型的绑定
        protected override void ConfigureViewModelLocator()
        {
            base.ConfigureViewModelLocator();
          
            ViewModelLocationProvider.Register<MainWindow, MainWindowViewModel>();
            ViewModelLocationProvider.Register<TreeListView, TreeListViewViewModel>();
            ViewModelLocationProvider.Register<DataGridView, DataGridViewModel>();
            ViewModelLocationProvider.Register<DataGridExpanderView, DataGridExpanderViewModel>();
        }
    }

}
